import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import CustomDialog from "@/components/stationProductionPage/HntManage/pouringOrder/WBS/index.vue";
import {
    getSteelProtectionWitnessByFid, //详情
    getSteelProtectionWitnessByAdd,//根据受检单位查询编号接口
    saveSteelProtectionWitness, //保存

    getBaseSgCompanyList,//机构树---只查施工单位
    getCheckitemsList, //试验检测依据-获取试验依据，检测依据
} from "@/api/request.js";

export default {
    props: [
        "dialogVisible", //弹框是否展示
        "dialogTitle", //标题
        "statusNum", //按钮编号 1新增   2编辑取样   3编辑收样  7查看
        "currentRow", //当前行
        "getList",
    ],
    data() {
        return {
            loading: false,
            visible: "", //弹框展示隐藏
            title: null, //弹框标题
            isLock: false, //取样按钮是否显示
            dialogHeight: null, //弹框高度
            companyTree: null,//机构树
            timeYMD: null, //当前日期
            timeYmdHms: null,
            btnShow: true,
            formObj: formJs.formObj,

            formData: {
                fId: "",
                userId: "",  //当前登录人id
                userName: "", //当前登录人姓名
                fEntrustno: "", //委托单编号
                fProjectid: "", //项目id 隐藏
                fProjectname: "", //项目简称
                fEntrustcompanyid: "", //委托单位id
                fEntrustcompanyname: "", //委托单位
                fEntrustuserid: "", //委托人id
                fEntrustusername: "", //委托人
                fCompanyid: "",//受检单位id
                fCompanyname: "", //受检单位
                fEntrustmobile: "", //委托人联系方式
                fProjectuse: "", //工程名称
                fWitnessusername: "", //见证人
                fWitnessunit: "", //见证单位
                fWitnessmobile: "", //见证人联系方式
                fChecktype: "", //检测类别
                fProjecttypename: "", //现场桩号或结构部位
                fProjecttypeid: "", //现场桩号或结构部位id
                fWitnessndates: "", //委托日期
                fSpecimenage: "", //龄期

                fThicknessdesignvalue: "",//设计厚度
                fAllowdeviation: "",//允许偏差1(+)
                fAllowdeviation1: "",//允许偏差2(-)

                fTestnote: "", //检测依据
                fJudgetype: "", //判定依据
                fTestnote1: [], //检测依据
                fJudgetype1: [], //判定依据
                fTestresult: "", //检测参数
                fTestprice: "", //检测费用
                fNote: "" //附加说明
            },//参数
            selected: [], //多选接值


            userInfo: {}, //登录用户
            newRowData: null,
            // WBS
            WBSShow: false,// WBS框显示隐藏
            wbsData: null,// WBS数据
            // WBS

        };
    },
    components: {
        publicForm,
        CustomDialog,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.newRowData = newVal;
                }
            },
            immediate: true,
        },
        getList: {
            handler(newVal) {
                if (newVal) {
                    // this.getList = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
        this.getData();
        this.getCheckitemsList("5"); //钢保检测依据
        this.getCheckitemsList("6"); //钢保判定依据
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        // console.log("this.userInfo", this.userInfo);
        this.btnShow = true;
        this.formObj.map(item => {
            item.disabled = false;
        });
        this.formObj[0].disabled = true;
        this.formObj[1].disabled = true;
        this.formObj[2].disabled = true;
        this.formObj[5].disabled = true;

        if (this.statusNum == 1) { //新增
            this.getBaseTree();
            this.formData = {
                fId: "",
                userId: this.userInfo.userId,  //当前登录人id
                userName: this.userInfo.realName, //当前登录人姓名
                fEntrustno: "", //委托单编号
                fProjectid: "", //项目id 隐藏
                fProjectname: "", //项目简称
                fEntrustcompanyid: this.userInfo.companyId, //委托单位id
                fEntrustcompanyname: this.userInfo.companyName, //委托单位
                fEntrustuserid: this.userInfo.userId, //委托人id
                fEntrustusername: this.userInfo.realName, //委托人
                fCompanyid: "",//受检单位id
                fCompanyname: "", //受检单位
                fEntrustmobile: "", //委托人联系方式
                fProjectuse: this.userInfo.projectName, //工程名称
                fWitnessusername: "", //见证人
                fWitnessunit: "", //见证单位
                fWitnessmobile: "", //见证人联系方式
                fChecktype: "", //检测类别
                fProjecttypename: "", //现场桩号或结构部位
                fProjecttypeid: "", //现场桩号或结构部位id
                fWitnessndates: this.timeYMD, //委托日期
                fSpecimenage: "", //龄期

                fThicknessdesignvalue: "",//设计厚度
                fAllowdeviation: "",//允许偏差1(+)
                fAllowdeviation1: "",//允许偏差2(-)

                fTestnote: "", //检测依据
                fJudgetype: "", //判定依据
                fTestnote1: [], //检测依据
                fJudgetype1: [], //判定依据
                fTestresult: "", //检测参数
                fTestprice: "", //检测费用
                fNote: "" //附加说明
            }
        } else if (this.statusNum == 2) { //2编辑
            this.getBaseTree();
            this.getDetail({
                fId: this.newRowData.fId
            })
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false //按钮
            this.formObj.map(item => {
                item.disabled = true;
            });
            this.getDetail({
                fId: this.newRowData.fId
            }) // currentRow
        }
        this.getHeight();

    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);
            // 受检单位
            if (b == "fCompanyname") {
                this.formObj[3].options.map(item => {
                    if (data == item.label) {
                        this.formData.fCompanyname = item.label;
                        this.formData.fCompanyid = item.value;
                        this.getEntrustno();
                    }
                })
            }
            // 委托日期
            if (b == "fWitnessndates") {
                if (this.formData.fCompanyid != "" && this.formData.fCompanyid != null) {
                    this.getEntrustno();
                }
            }
            // 检测依据
            if (b == 'fTestnote1') {
                var arr = []
                data.map(item => {
                    if (item.label) {
                        arr.push(item.label)
                    } else {
                        arr.push(item)
                    }
                })
                this.formData.fTestnote = arr.join(",");
            }
            // 判定依据
            if (b == 'fJudgetype1') {
                var arr1 = []
                data.map(item => {
                    if (item.label) {
                        arr1.push(item.label)
                    } else {
                        arr1.push(item)
                    }

                })
                this.formData.fJudgetype = arr1.join(",")
            }
        },
        inputFocus(index, prop) {
            // console.log("inputFocus", index, prop);
            return index, prop;
        },
        clickDialog(index, prop) {
            // console.log("clickDialog", index, prop);
            // 现场桩号或结构部位
            if (prop == "fProjecttypename") {
                this.WBSShow = true;
            }
            return index, prop;
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        // 详情
        getDetail(obj) {
            this.loading = true;
            getSteelProtectionWitnessByFid(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.formData = res.data.yjQmpTestSteelprotectiontestwitness;
                        //检测依据
                        if (res.data.yjQmpTestSteelprotectiontestwitness.fTestnote != '') {
                            var fTestnote = [];
                            this.formObj[16].options.map(item => {
                                res.data.yjQmpTestSteelprotectiontestwitness.fTestnote.split(",").map(i => {
                                    if (item.label == i) {
                                        fTestnote.push(item);
                                    }
                                })
                            })
                        } else {
                            fTestnote = [];
                        }
                        //检测依据

                        //判定依据
                        if (res.data.yjQmpTestSteelprotectiontestwitness.fJudgetype != '') {
                            var fJudgetype = [];
                            this.formObj[17].options.map(item => {
                                res.data.yjQmpTestSteelprotectiontestwitness.fJudgetype.split(",").map(i => {
                                    if (item.label == i) {
                                        fJudgetype.push(item);
                                    }
                                })
                            })
                        } else {
                            fJudgetype = [];
                        }
                        //判定依据
                        this.selected = [
                            {
                                name: "fTestnote1",
                                selected: fTestnote,
                            },
                            {
                                name: "fJudgetype1",
                                selected: fJudgetype,
                            },
                        ];
                    } else {
                        this.loading = false;
                        this.$message.error(res.msg);
                    }
                })
                .catch(() => {
                    this.loading = false;
                    console.log("取样详情接口报错");
                });
        },


        yj_saveForm() {
            if (this.$refs.publicForm.submitForm()) {
                var data = {
                    fId: this.formData.fId,
                    userId: this.userInfo.userId,  //当前登录人id
                    userName: this.userInfo.realName, //当前登录人姓名
                    fEntrustno: this.formData.fEntrustno, //委托单编号
                    fProjectid: this.formData.fProjectid, //项目id 隐藏
                    fProjectname: this.formData.fProjectname, //项目简称
                    fEntrustcompanyid: this.formData.fEntrustcompanyid, //委托单位id
                    fEntrustcompanyname: this.formData.fEntrustcompanyname, //委托单位
                    fEntrustuserid: this.formData.fEntrustuserid,//委托人id
                    fEntrustusername: this.formData.fEntrustusername, //委托人
                    fCompanyid: this.formData.fCompanyid,//受检单位id
                    fCompanyname: this.formData.fCompanyname, //受检单位
                    fEntrustmobile: this.formData.fEntrustmobile, //委托人联系方式
                    fProjectuse: this.formData.fProjectuse,//工程名称
                    fWitnessusername: this.formData.fWitnessusername, //见证人
                    fWitnessunit: this.formData.fWitnessunit, //见证单位
                    fWitnessmobile: this.formData.fWitnessmobile,//见证人联系方式
                    fChecktype: this.formData.fChecktype,//检测类别
                    fProjecttypename: this.formData.fProjecttypename, //现场桩号或结构部位
                    fProjecttypeid: this.formData.fProjecttypeid,//现场桩号或结构部位id
                    fWitnessndates: this.formData.fWitnessndates,//委托日期
                    fSpecimenage: this.formData.fSpecimenage, //龄期

                    fThicknessdesignvalue: this.formData.fThicknessdesignvalue,//设计厚度
                    fAllowdeviation: this.formData.fAllowdeviation,//允许偏差1(+)
                    fAllowdeviation1: this.formData.fAllowdeviation1,//允许偏差2(-)

                    fTestnote: this.formData.fTestnote, //检测依据
                    fJudgetype: this.formData.fJudgetype,//判定依据
                    fTestresult: this.formData.fTestresult, //检测参数
                    fTestprice: this.formData.fTestprice, //检测费用
                    fNote: this.formData.fNote, //附加说明
                };
                this.saveForm(data);
            } else {
                console.log("error");
            }
        },

        // 保存
        saveForm(data) {
            if (this.wbsData != null && this.wbsData != "") {
                if (this.wbsData.fLongname != data.fProjecttypename) {
                    data.fProjecttypeid = "";
                }
            }
            this.loading = true;
            this.isLock = true;
            saveSteelProtectionWitness(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.$emit("refreshList");
                        this.isLock = false;
                        this.visible = false;
                        this.$message.success("保存成功");
                    } else {
                        this.loading = false;
                        this.isLock = false;
                        this.$message.error(res.msg);
                    }
                })
                .catch(() => {
                    console.log("保存接口报错");
                });
        },

        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },
        //查受检单位
        getBaseTree() {
            var data = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId
            }
            this.formObj[3].options = [];
            getBaseSgCompanyList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.userCompanyTreeList;

                        data.map(item => {
                            this.formObj[3].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        //获取委托单编号
        getEntrustno() {
            var data = {
                fCompanyid: this.formData.fCompanyid,//受检单位
                fWitnessndates: this.formData.fWitnessndates, //委托时间  不传默认当前日期
            }
            getSteelProtectionWitnessByAdd(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.yjQmpTestSteelprotectiontestwitness;
                        // console.log("获取委托单编号", data);
                        this.formData.fEntrustno = data.fEntrustno;
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // WBS
        refreshList() { },
        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        getWBS(data) { //获取选择WBS信息
            this.wbsData = data;
            this.formData.fProjecttypename = data.fLongname; //现场桩号或结构部位
            this.formData.fProjecttypeid = data.fNodeid; //现场桩号或结构部位id
        },
        // WBS



        // 试验检测依据-获取试验依据，检测依据
        getCheckitemsList(num) {
            getCheckitemsList({
                modelType: num,
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        if (num == 5) { //检测依据
                            this.formObj[16].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[16].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })
                        } else if (num == 6) { //判定依据
                            this.formObj[17].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[17].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId
                                })
                            })
                        }
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },

    },
};